﻿using HAF.DbUtility.Enum;
using HAF.DBUtility;
using HAF.LogUtility;
using Iptv.MediaReporter.Entity;
using System;
using System.Collections.Generic;

namespace Iptv.MediaReporter.Helper
{
    /// <summary>
    /// 上报数据提取类
    /// </summary>
    public class ReportDbHelper : DbBaseHelper
    {
        private static ReportDbHelper _dbhelper = null;
        private ReportDbHelper() { }

        public static ReportDbHelper GetInstance()
        {
            return _dbhelper ?? (_dbhelper = new ReportDbHelper());
        }

        /// <summary>
        /// 获取待采集指纹数据集
        /// </summary>
        /// <returns></returns>
        public List<MediaEntity> GetMedia(string date, int counter)
        {
            if (string.IsNullOrEmpty(date))
            {
                return null;
            }
            if (!DateTime.TryParse(date, out DateTime bDate))
            {
                return null;
            }
            DateTime eDate = bDate.AddDays(1);
            if (counter == 0)
            {
                counter = 100;
            }
            //SELECT program_out_source_id,file_name, file_size,play_url,file_path, storage_path as LocalFullPath, Create_time as CreateTime  FROM cms_pre_programmedia ORDER BY Create_time DESC LIMIT 10;
            var db = GetReadDB();
            string sql = string.Empty;
            try
            {
                db.Begin();
                sql = string.Format("SELECT out_source_id_two as MediaId, program_out_source_id as MediaCode,file_name as MediaName, file_size as FileSize, play_url, file_path, storage_path as RemoteFullPath, create_time as CreateTime " +
                    "FROM cms_pre_programmedia where (create_time between '{0}' and '{1}') AND cp_code = 'public' ORDER BY Create_time DESC LIMIT {2};", 
                    bDate.ToString("yyyy-MM-dd"), eDate.ToString("yyyy-MM-dd"), counter);
                LogHelper.WriteDebugLog(sql);
                List<MediaEntity> outDatas = db.Query<MediaEntity>(sql);
                db.Commit();
                
                return outDatas;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(string.Format("方法{0}发生异常：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString()));
                return null;
            }
        }

        /// <summary>
        /// 获取待采集指纹数据集
        /// </summary>
        /// <param name="code">内容Code</param>
        /// <returns></returns>
        public List<MediaEntity> GetMedia(string code)
        {
            //SELECT program_out_source_id,file_name, file_size,play_url,file_path, storage_path as LocalFullPath, Create_time as CreateTime  FROM cms_pre_programmedia ORDER BY Create_time DESC LIMIT 10;
            string sql = string.Empty;
            var db = GetReadDB();
            try
            {
                db.Begin();
                sql = string.Format("SELECT out_source_id_two as MediaId, program_out_source_id as MediaCode,file_name as MediaName, file_size as FileSize, " +
                    "play_url, file_path, storage_path as RemoteFullPath, create_time as CreateTime FROM cms_pre_programmedia " +
                    "where program_out_source_id ='{0}' ORDER BY Create_time DESC;", code);
                LogHelper.WriteDebugLog(sql);
                List<MediaEntity> outDatas = db.Query<MediaEntity>(sql);
                db.Commit();

                return outDatas;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(string.Format("方法{0}发生异常：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString()));
                return null;
            }
        }

        /// <summary>
        /// 获取剧头信息
        /// </summary>
        /// <param name="date">取值日期</param>
        /// <returns></returns>
        public List<ProgramEntity> GetProgramDatas(string date)
        {
            //单集和多集剧头 单集剧头可直接到媒资表取介质信息 多集剧头需到映射表取子集数据集后再去取介质数据
            //一般情况下 MediaType 0 为单集 1 为多集 也可加上Counter数量 1 为单集 大于1的为多集
            if (string.IsNullOrEmpty(date))
            {
                return null;
            }
            if (!DateTime.TryParse(date, out DateTime bDate))
            {
                return null;
            }
            DateTime eDate = bDate.AddDays(1);
            //string sql = "SELECT programset_name AS ProgramName,out_source_id AS MediaCode,cp_code AS CPName,IFNULL(series_type,0) AS MediaType," +
            //    "out_source_id_two,IFNULL(program_total_count, 1) as counter, years,content_provider,release_date FROM cms_pre_programset" +
            //    " WHERE cp_code != 'ASVideo' and  (create_time BETWEEN '2024-12-23' AND '2024-12-24')";
            string sql = string.Empty;
            var db = GetReadDB();
            try
            {
                db.Begin();
                sql = string.Format("SELECT programset_name AS ProgramName,out_source_id AS MediaCode,cp_code AS CPName,IFNULL(series_type,0) AS MediaType,out_source_id_two as MediaID," +
                "IFNULL(program_total_count, 1) as Counter, years as ReleaseYear,content_provider as ContentProvider,release_date as ReleaseDate,certificate_no AS LicenseNum,create_time as CreateTime " +
                "FROM cms_pre_programset WHERE (cp_code = 'LTVideo' or cp_code = 'public') and  (create_time BETWEEN '{0}' AND '{1}');",  // limit 6
                bDate.ToString("yyyy-MM-dd"), eDate.ToString("yyyy-MM-dd"));
                LogHelper.WriteDebugLog(sql);
                List<ProgramEntity> outDatas = db.Query<ProgramEntity>(sql);
                db.Commit();

                return outDatas;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(string.Format("方法{0}发生异常：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString()));
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<ProgramEntity> GetSubProgramDatas(string code)
        {
            string sql = string.Empty;
            var db = GetReadDB();
            try
            {
                db.Begin();
                sql = string.Format("SELECT num AS Num, program_out_source_id AS MediaCode, program_out_source_id_two AS MediaID, programset_out_source_id AS SeriesCode," +
                    "programset_out_source_id_two AS SeriesID FROM cms_pre_programset_program_map WHERE programset_out_source_id = '{0}' ORDER BY num;", code);
                LogHelper.WriteDebugLog(sql);
                List<ProgramEntity> outDatas = db.Query<ProgramEntity>(sql);
                db.Commit();

                return outDatas;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(string.Format("方法{0}发生异常：{1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString()));
                return null;
            }
        }
    }
}
